﻿#include "excelreader.h"
#include <QFile>
#include <QMessageBox>
#include <memory>
#include <QDebug>


ExcelReader::ExcelReader(QObject *parent)
    : QObject{parent}
{}

bool ExcelReader::openFile(const QString& filePath)
{
    bool ret = false;
    if(!filePath.isEmpty()) {
        QFile file(filePath);
        if(!file.open(QIODevice::ReadOnly)) {
            QMessageBox::warning(Q_NULLPTR, u8"警告", u8"文件" + filePath + u8"打开失败!");
            file.close();
        } else {
            file.close();
            p_doc = std::make_unique<QXlsx::Document>(filePath);
            if(!p_doc || !p_doc->load())ret = false;
            else ret = true;
        }
    }
    return ret;
}

QStringList ExcelReader::getSheetsName() const
{
    QStringList ret_list;
    if(p_doc) ret_list = p_doc->sheetNames();
    return ret_list;
}

bool ExcelReader::selectSheet(const QString& sheetName) const
{
    if(sheetName.isEmpty()) return false;
    return p_doc->selectSheet(sheetName);
}

QXlsx::Worksheet* ExcelReader::getWorkSheet() const
{
    return p_doc->currentWorksheet();
}

